package cn.iocoder.yudao.module.design.service.flowmd;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.flowmd.vo.FlowMdCreateReqVO;
import cn.iocoder.yudao.module.design.controller.admin.flowmd.vo.FlowMdExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.flowmd.vo.FlowMdPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.flowmd.vo.FlowMdUpdateReqVO;
import cn.iocoder.yudao.module.design.convert.flowmd.FlowMdConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.flowmd.FlowMdDO;
import cn.iocoder.yudao.module.design.dal.mysql.flowmd.FlowMdMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.FLOW_MD_NOT_EXISTS;

/**
 * 流程模板 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FlowMdServiceImpl implements FlowMdService {

    @Resource
    private FlowMdMapper flowMdMapper;

    @Override
    public Long createFlowMd(FlowMdCreateReqVO createReqVO) {
        // 插入
        FlowMdDO flowMd = FlowMdConvert.INSTANCE.convert(createReqVO);
        flowMdMapper.insert(flowMd);
        // 返回
        return flowMd.getId();
    }

    @Override
    public void updateFlowMd(FlowMdUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateFlowMdExists(updateReqVO.getId());
        // 更新
        FlowMdDO updateObj = FlowMdConvert.INSTANCE.convert(updateReqVO);
        flowMdMapper.updateById(updateObj);
    }

    @Override
    public void deleteFlowMd(Long id) {
        // 校验存在
        this.validateFlowMdExists(id);
        // 删除
        flowMdMapper.deleteById(id);
    }

    private void validateFlowMdExists(Long id) {
        if (flowMdMapper.selectById(id) == null) {
            throw exception(FLOW_MD_NOT_EXISTS);
        }
    }

    @Override
    public FlowMdDO getFlowMd(Long id) {
        return flowMdMapper.selectById(id);
    }

    @Override
    public List<FlowMdDO> getFlowMdList(Collection<Long> ids) {
        return flowMdMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FlowMdDO> getFlowMdPage(FlowMdPageReqVO pageReqVO) {
        return flowMdMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FlowMdDO> getFlowMdList(FlowMdExportReqVO exportReqVO) {
        return flowMdMapper.selectList(exportReqVO);
    }

    /**
     * 获得流程模板列表
     *
     * @param flowCd 编号
     * @return 流程模板列表
     */
    @Override
    public List<FlowMdDO> getFlowMdList(Integer flowCd) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("flow_cd",flowCd);
        return flowMdMapper.selectList(queryWrapper);
    }

}
